package io.zpy.controller;

import com.alibaba.fastjson.JSONObject;
import io.zpy.config.ErrorCodeEnum;
import io.zpy.config.Utils;
import io.zpy.entity.PushOrder;
import io.zpy.enums.StatusCode;
import io.zpy.service.TrackTopicsService;
import io.zpy.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 *
 * @author xxx
 * @date 2020/5/14
 */
@RestController
@RequestMapping("/track")
@Api("相似课题追踪")
public class TrackTopicsController {

    private final static Logger LOG = LoggerFactory.getLogger(TrackTopicsController.class);

    @Autowired
    private TrackTopicsService trackTopicsService;
    /**
     * 判断返回json是否包含成功
     */
    final String success = "成功";



    /**
     * 7-主页
     *
     * @return
     */
    @RequestMapping(path = "/ordersQuery")
    @ApiOperation("订单查询")
    public R getOwnPushOrders(//用户lid
                              @RequestParam(value = "lid", required = false) String lid) {

        try {
            JSONObject result = trackTopicsService.getOwnPushOrders(lid);
            return R.ok(result);
        } catch (Exception e) {
            LOG.error("存在异常：", e);
            return R.error(StatusCode.Exception.getCode(), StatusCode.Exception.getMsg());
        }
    }

    /**
     * 点击跳转修改页面
     *
     * @param pid
     * @return
     */
    @RequestMapping(value = "/editTrackInfo")
    public R moduleEditInfo(//订单id
                            @RequestParam(value = "pid") String pid,
                            //用户id
                            @RequestParam(value = "lid") String lid) {
        List<PushOrder> list = null;
        JSONObject result = new JSONObject();
        try {
            //入参校验
            if (StringUtils.isBlank(lid) || StringUtils.isBlank(pid)) {
                return R.error(StatusCode.Invalid_Params.getCode(), StatusCode.Invalid_Params.getMsg());
            }
            list = trackTopicsService.getPushOrderByPid(lid, pid);
//            list = Utils.formatList(list);
            result.put("msg", StatusCode.Success.getMsg());
            result.put("code", StatusCode.Success.getCode());
            result.put("results", list);
            return R.ok(result);
        } catch (Exception e) {
            e.printStackTrace();
            return R.error(ErrorCodeEnum.INVOKE_FAILED.getCode(), ErrorCodeEnum.INVOKE_FAILED.getDesc());
        }
    }

    /**
     * 点击编辑订单信息
     *
     * @param pid
     * @param workOrder
     * @param keywords
     * @param toAddress
     * @param frequency
     * @param trackAmount
     * @param lid
     * @return
     */
    @RequestMapping(value = "/updateByPid")
    @Transactional(rollbackFor = {Throwable.class})
    @ApiOperation("编辑订单")
    public R editTrackInfo(//用户id
                           @RequestParam(value = "lid") String lid,
                           //订单id
                           @RequestParam(value = "pid") String pid,
                           //订单名称
                           @RequestParam(value = "workOrder", required = false) String workOrder,
                           //订单关键词
                           @RequestParam(value = "keywords", required = false) String keywords,
                           //收件人
                           @RequestParam(value = "toAddress", required = false) String toAddress,
                           //追踪频率
                           @RequestParam(value = "frequency") String frequency,
                           //追踪数量
                           @RequestParam(value = "trackAmount") String trackAmount
    ) {
        PushOrder po = new PushOrder();
        JSONObject result = new JSONObject();
        R r = null;
        try {
            //入参校验
            if (StringUtils.isBlank(pid) || StringUtils.isBlank(lid) || (StringUtils.isBlank(workOrder) && StringUtils.isBlank(keywords) && StringUtils.isBlank(toAddress) && StringUtils.isBlank(frequency) && StringUtils.isBlank(trackAmount))) {
                return R.error(StatusCode.Invalid_Params.getCode(), StatusCode.Invalid_Params.getMsg());
            }
            po.setPid(Integer.parseInt(pid));
            po.setWorkOrder(workOrder);
            po.setKeywords(keywords);
            po.setToAddress(toAddress);
            po.setFrequency(frequency);
            po.setTrackAmount(trackAmount);
            po.setUid(lid);
            po.setUpdateDate(Utils.getDateFormat());

            if (trackTopicsService.editPushOrder(po) > 0) {
                r = R.ok(ErrorCodeEnum.UPDATE_SUCCESS.getDesc());
            } else {
                r = R.error(ErrorCodeEnum.UPDATE_FAILED.getCode(), ErrorCodeEnum.UPDATE_FAILED.getDesc());
            }
        } catch (Exception e) {
            e.printStackTrace();
            r = R.error(StatusCode.Exception.getCode(), StatusCode.Exception.getMsg());
            throw new RuntimeException();
        }
        return r;
    }

    //点击删除订单信息
    @RequestMapping(value = "delTrackInfo")
    @Transactional(rollbackFor = {Throwable.class})
    @ApiOperation("删除订单")
    public R delTrackInfo(//用户id
                          @RequestParam(value = "lid") String lid,
                          //订单id
                          @RequestParam(value = "pid") String pid) {
        R r = null;
        try {
            //入参校验
            if (StringUtils.isBlank(lid)) {
                return R.error(StatusCode.Invalid_Params.getCode(), StatusCode.Invalid_Params.getMsg());
            }
            if (trackTopicsService.delPushOrder(Integer.parseInt(pid)) > 0) {
                r = R.ok(ErrorCodeEnum.DELETE_SUCCESS.getDesc());
            } else {
                r = R.error(ErrorCodeEnum.DELETE_FAILED.getCode(), ErrorCodeEnum.DELETE_FAILED.getDesc());
            }
        } catch (Exception e) {
            e.printStackTrace();
            r = R.error(StatusCode.Exception.getCode(), StatusCode.Exception.getMsg());
            throw new RuntimeException();
        }
        return r;
    }

    //点击增加订单信息
    @RequestMapping(value = "addTrackInfo")
    @Transactional(rollbackFor = {Throwable.class})
    @ApiOperation("添加订单")
    public R addTrackInfo(//用户id
                          @RequestParam(value = "lid", required = false) String lid,
                          //订单名称
                          @RequestParam(value = "workOrder", required = false) String workOrder,
                          //订单关键词
                          @RequestParam(value = "keywords") String keywords,
                          //收件人
                          @RequestParam(value = "toAddress") String toAddress,
                          //追踪频率
                          @RequestParam(value = "frequency") String frequency,
                          //追踪数量
                          @RequestParam(value = "trackAmount") String trackAmount
    ) {
        R r = null;
        PushOrder po = new PushOrder();
        try {
            //入参校验
            if (StringUtils.isBlank(lid) || StringUtils.isBlank(toAddress) || StringUtils.isBlank(frequency) || StringUtils.isBlank(trackAmount)) {
                return R.error(StatusCode.Invalid_Params.getCode(), StatusCode.Invalid_Params.getMsg());
            }
            po.setWorkOrder(workOrder);
            po.setKeywords(keywords);
            po.setToAddress(toAddress);
            po.setFrequency(frequency);
            po.setTrackAmount(trackAmount);
            po.setUid(lid);
            po.setCreateDate(Utils.getDateFormat());

            if (trackTopicsService.addPushOrder(po) > 0) {
                r = R.ok(ErrorCodeEnum.ADD_SUCCESS.getDesc());
            } else {
                r = R.error(ErrorCodeEnum.ADD_FAILED.getCode(), ErrorCodeEnum.ADD_FAILED.getDesc());
            }
        } catch (Exception e) {
            e.printStackTrace();
            r = R.error(StatusCode.Exception.getCode(), StatusCode.Exception.getMsg());
            throw new RuntimeException();
        }
        return r;
    }
}
